import QtQuick 1.1
import com.meego 1.0
import "Modulelib.js" as ModuleUtil
import "CommomUtil.js" as Util

Page {
    id: dealer_info_page

    tools: ModuleToolBar{}

    orientationLock : PageOrientation.LockPortrait

    property string dealerName: ""

    property string dealerId: "0"

    property string phoneCall: ""

    property string glocation: ""

     TitleBar{
            id: module_detail_title
            anchors.top: dealer_info_page.top; anchors.left: dealer_info_page.left
            width: dealer_info_page.width; height: 71
            titleName: ModuleUtil.current_dealer_name
        }

     Rectangle{
         id: info_show_rect
         width: dealer_info_page.width; height: 235
         anchors{left: parent.left; top: module_detail_title.bottom}
         Rectangle{
             id: info_show1
             gradient: Gradient {
                 GradientStop { position: 0.0; color: "white" }
                 GradientStop { position: 0.45; color: "#c0c5c6" }
                 GradientStop { position: 0.55; color: "#c0c5c6" }
                 GradientStop { position: 1.0; color: "white" }
             }
             border{color: "#c0c5c6"; width: 1}
             anchors{fill: parent; margins: 20}
             radius: 20
             Row{
                 Item{
                     width: 20; height: 235
                 }

             Column{
                 spacing: 9
                 Item{
                     width: dealer_info_page.width; height: 15
                 }
                 Text {
                     id: dealer_name
                     text: dealer_info_page.dealerName
                     font{bold: true; pointSize: 18}
                     color: "#000000"
                     horizontalAlignment: Text.AlignLeft
                 }
                 Row{
                     spacing: 5
                     Text{
                         text: "经销厂商:"
                         font{bold: true; pointSize: 15}
                         color: "#abb2b3"
                     }
                     Text{
                         id: dealer_simname
                         text: ""
                         font{bold: true; pointSize: 18}
                         color: "#778fd1"
                     }
                 }
                 Row{
                     spacing: 5
                     Text{
                         text: "公司地址:"
                         font{bold: true; pointSize: 14}
                         color: "#abaeb3"
                         wrapMode: Text.WordWrap
                     }
                     Text{
                         id: dealer_addr
                         text: ""
                         wrapMode: Text.WordWrap
                         font{bold: true; pointSize: 18}
                         color: "#778fd1"
                     }
                 }
                 Row{
                     spacing: 5
                     Text{
                         text: "公司电话:"
                         font{bold: true; pointSize: 14}
                         color: "#abb2b3"
                     }
                     Text{
                         id: dealer_phone
                         text: ""
                         font{bold: true; pointSize: 18}
                         color: "#778fd1"
                     }
                 }
             }
             Item{
                 width: 20; height: 235
             }
         }
     }
 }

     Rectangle{
         id: link_rect
         width: parent.width; height: 68
         anchors{left: parent.left; top: info_show_rect.bottom}
         Rectangle{
            id: link_rect1
            anchors{fill: parent; leftMargin: 20; rightMargin: 20}
            Row{
                spacing: 10
                Rectangle{
                    id: phone_rect
                    width: 210; height: 68
                    Image {
                        id: phone_call
                        width: 210; height: 68
                        source: "../images/modelslib/call.png"

                    }
                    MouseArea{
                        id: phoneArea
                        anchors.fill: parent
                        onClicked: {// 转向拨号页面
                            Qt.openUrlExternally("tel:"+dealer_info_page.phoneCall);
                        }
                    }

                    states: [
                        State {
                            name: 'pressed'; when: phoneArea.pressed
                            PropertyChanges { target: phone_rect; focus: true; opacity: 0.5; color: "#000"}
                        }
                    ]
                }

                Rectangle{
                    id: map_rect
                    width: 210; height: 68


                Image {
                    id: map_call
                    source: "../images/modelslib/map.png"
                    width:210; height: 68

                }
                MouseArea{
                    id: mapArea
                    anchors.fill: parent
                    onClicked: { // 转向GMap 页面
                        ModuleUtil.dealer_location = dealer_info_page.glocation;
                        Util.openFile("DealerMapPage.qml");
                    }
                }

                states: [
                    State {
                        name: 'pressed'; when: mapArea.pressed
                        PropertyChanges { target: map_rect; focus: true; opacity: 0.5; color: "#000"}
                    }
                ]

             }
            }
         }
     }


     Rectangle{
         id: show_vehicles_rect
         width: parent.width; height: 108
         anchors{left: parent.left; top: link_rect.bottom}
         Rectangle{
             id: show_vehicles_rect1
             anchors{fill: parent; margins: 20}
             radius: 20
             gradient: Gradient {
                 GradientStop { position: 0.0; color: "white" }
                 GradientStop { position: 0.5; color: "#f2f3e8" }
                 GradientStop { position: 1.0; color: "white" }
             }
             border{color: "#abb2b3"; width: 1}
             Text {
                 id: show_vehicles
                 text: "查看全部车型报价"
                 font{bold: true; pointSize: 18}
                 color: "#000000"
                 verticalAlignment: Text.AlignVCenter
                 anchors{centerIn: parent}
             }

             MouseArea{
                 id: showArea
                 anchors.fill: parent
                 onClicked: { //查看全部车型报价
                     Util.openFile("DealerCarsPage.qml");
                 }
             }

             states: [
                 State {
                     name: 'pressed'; when: showArea.pressed
                     PropertyChanges { target: show_vehicles_rect1; focus: true; opacity: 0.5; color:"#000"}
                 }
             ]
         }
     }


     BusyLoading{
         id: loading
         anchors.centerIn: parent
         visible:false
     }

     Component.onCompleted: {
         loading.loading();
         dealer_info_page.getData();
     }

     function getData(){
         Util.getXmlStringByUrl(ModuleUtil.getDealerDetailUrl(),callback);
     }

     function callback(responseXml){
         var a = responseXml.documentElement;
         console.log("dealer length:"+a.childNodes[0].childNodes.length);
         console.log("dealer node name:"+a.childNodes[0].nodeName);
         dealer_info_page.dealerId = a.childNodes[0].childNodes[0].childNodes[0].nodeValue;
         dealer_info_page.dealerName = a.childNodes[0].childNodes[1].childNodes[0].nodeValue;
         dealer_simname.text = a.childNodes[0].childNodes[2].childNodes[0].nodeValue;
         dealer_addr.text = a.childNodes[0].childNodes[4].childNodes[0].nodeValue;
         dealer_phone.text = a.childNodes[0].childNodes[6].childNodes[0].nodeValue;
         var phoneNum = a.childNodes[0].childNodes[7].childNodes[0].nodeValue;
         var pNumArray = phoneNum.split(",");
         if(pNumArray.length > 0){
             dealer_info_page.phoneCall = pNumArray[0];
         }else{
             dealer_info_page.phoneCall = phoneNum;
         }
         var map_location = a.childNodes[0].childNodes[8].childNodes[0].nodeValue;
         var locArray = map_location.split(",");
         if(locArray !== null && locArray.length > 1){
             dealer_info_page.glocation = locArray[1] + "," + locArray[0];
         }
         loading.finish();

     }

}
